#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash juicer-1.6.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
[[ "$buildpath" == "$installpath" ]] && echo "构建路径与安装路径需为不同路径" && exit 1
yum install -y  environment-modules csh time patch libatomic perl-Data-Dumper autoconf automake libtool numactl binutils systemd-devel valgrind flex wget

set -e
sudo yum install -y tar Cython cmake git time
source /etc/profile

cd $buildpath
  echo "# gridss正在下载"

wget -t 10 -T 20 -c -O $buildpath/gridss-2.13.2.tar.gz \
    https://github.com/PapenfussLab/gridss/archive/refs/tags/v2.13.2.tar.gz

 if [[ $? -eq 0 ]]; then
    echo -e "\033[1;32;1m# gridss已经下载成功\033[0m"
  else
    echo -e "\033[31m# gridss下载失败,请重新执行脚本\033[0m"
    exit 1
  fi

  echo "# gridss正在安装"
  tar -xzf $buildpath/gridss-2.13.2.tar.gz -C $buildpath && rm -rf $buildpath/gridss-2.13.2.tar.gz
  wget -t 40  -c -P $buildpath https://github.com/PapenfussLab/gridss/releases/download/v2.13.2/gridss-2.13.2-gridss-jar-with-dependencies.jar

  cp -r $buildpath/gridss-2.13.2-gridss-jar-with-dependencies.jar $buildpath/gridss-2.13.2/scripts
  cd $buildpath/gridss-2.13.2/scripts
  chmod 777 gridss
  sed -i '1205,1207d' gridss
  echo -e "\033[1;32;1m# gridss已经安装完毕\033[0m"

  mkdir -p $installpath/gridss-2.13.2 && cp -r $buildpath/gridss-2.13.2/* $installpath/gridss-2.13.2

  cat >"$installpath/gridss-2.13.2/gridss_modulefiles" <<EOF
#%Module1.0
conflict gridss
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
  variable modfile
  set modfile_path [file dirname \$modfile]
  return \$modfile_path
}
set pwd [getModulefileDir]
set GRIDSS \$pwd
setenv GRIDSS \$GRIDSS
prepend-path PATH \$GRIDSS/scripts
EOF

  rm -rf $buildpath/gridss-2.13.2
